﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Security.Permissions;
using System.IO;
using System.Web.Caching;
using System.Configuration;

namespace Vision.Utilities.ConsoleApp.PreCompilation
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class CmsVirtualFile : VirtualFile
    { 
        #region Ctor
        public CmsVirtualFile(string virtualPath)
            : base(virtualPath)
        {
            Init(virtualPath);
        }
        #endregion

        #region Init
        private void Init(string virtualPath)
        {
            // Not Including "~"
            if (CmsVirtualPathProvider.IsSpecialPath(virtualPath))
            {
                PhysicalPath = HostingEnvironment.MapPath(virtualPath);
            } 
        }
        #endregion

        #region Properties
        public string PhysicalPath { get; private set; }
        #endregion

        #region Override
        public override System.IO.Stream Open()
        {
            // Try to get the stream out of the cache.
            //var cachedBuffer = (byte[])HostingEnvironment.Cache.Get(PhysicalPath);
            //if (cachedBuffer == null)
            //{
            //    using (var fileReader = File.OpenRead(PhysicalPath))
            //    {
            //        var length = 1024;
            //        var count = 0;
            //        cachedBuffer = new byte[fileReader.Length];
            //        count = fileReader.Read(cachedBuffer, 0, length);
            //        while (count > 0)
            //        {
            //            count = fileReader.Read(cachedBuffer, 0, length);
            //        }
            //    }
            //    var cacheDependency = new CacheDependency(PhysicalPath);
            //    HostingEnvironment.Cache.Add(PhysicalPath, cachedBuffer, cacheDependency, Cache.NoAbsoluteExpiration, new TimeSpan(1, 0, 0), CacheItemPriority.Default, null);
            //}
            //if (cachedBuffer != null)
            //{
            //    var stream = new MemoryStream();
            //    var binaryWriter = new BinaryWriter(stream);
            //    binaryWriter.Write(cachedBuffer, 0, cachedBuffer.Length);
            //    binaryWriter.Flush();
            //    stream.Seek(0, SeekOrigin.Begin);
            //    return stream;
            //}
            //return null;

            var stream = new MemoryStream();
            var binaryWriter = new BinaryWriter(stream);
            using (var fileReader = File.OpenRead(PhysicalPath))
            {
                var length = 1024;
                var count = 0;
                var buffer = new byte[length];
                count = fileReader.Read(buffer, 0, length);
                while (count > 0)
                {
                    binaryWriter.Write(buffer, 0, count);
                    count = fileReader.Read(buffer, 0, length);
                }
                binaryWriter.Flush();
            }
            stream.Seek(0, SeekOrigin.Begin);
            return stream;
        }
     
        public bool Exists
        {
            get
            {
                return !string.IsNullOrEmpty(PhysicalPath) && File.Exists(PhysicalPath);
            }
        }
        
        public override bool IsDirectory
        {
            get
            {
                return !string.IsNullOrEmpty(PhysicalPath) && Directory.Exists(PhysicalPath);
            }
        }
         
        #endregion
    }
}